package com.tbs.parkingsaleapp.Http;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;

public class AsycHttp<TP,TR> extends  AsyncTask<TP,Double, HttpResult<TR>>
{
    private Context context;
    private IAsycHttpErrorAction<TP,TR> errorAction=new DefaultErrorHandle<>();
    private IAsycHttpBackgroundAction<TP,TR> backgroundAction;
    private IAsycHttpResultAction<TR> resultAction;
    private IAsycHttpProgressUpdateAction progressUpdateAction;


    public IAsycHttpErrorAction<TP,TR> getErrorAction() {
        return errorAction;
    }

    public AsycHttp<TP,TR> setErrorAction(IAsycHttpErrorAction<TP,TR> errorAction) {
        this.errorAction = errorAction;
        return this;
    }

    public IAsycHttpProgressUpdateAction getProgressUpdateAction() {
        return progressUpdateAction;
    }

    public IAsycHttpResultAction<TR> getResultAction() {
        return resultAction;
    }

    public IAsycHttpBackgroundAction<TP, TR> getBackgroundAction() {
        return backgroundAction;
    }

    public AsycHttp<TP,TR> setProgressUpdateAction(IAsycHttpProgressUpdateAction progressUpdateAction) {
        this.progressUpdateAction = progressUpdateAction;
        return this;
    }

    public AsycHttp<TP,TR> setResultAction(IAsycHttpResultAction<TR> resultAction) {

        this.resultAction = resultAction;
        return this;
    }

    public AsycHttp<TP,TR> setBackgroundAction(IAsycHttpBackgroundAction<TP,TR> backgroundAction) {
        this.backgroundAction = backgroundAction;
        return this;
    }

    public AsycHttp<TP,TR> setContext(Context context) {
        this.context = context;
        return this;
    }

    public Context getContext() {
        return context;
    }

    public void PushProgress(double value)
    {
        this.publishProgress(value);

    }



    @Override
    protected void onProgressUpdate(Double... values)
    {
        if(progressUpdateAction!=null)
            progressUpdateAction.ProgressUpdateAction(getContext(),values);
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(HttpResult<TR> httpResult) {
        super.onPostExecute(httpResult);
        if(httpResult.getCode()==1)
        {
            if(resultAction!=null)
                resultAction.ResultFunction(httpResult.getResult());
        }
        else
        {
            if(errorAction!=null)
                errorAction.Handle(httpResult,this);
        }
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }
    @Override
    protected HttpResult<TR> doInBackground(TP... tps)
    {
        if(backgroundAction!=null)
        {
            HttpResult result=new HttpResult();
            TR obj;
            try {
                  obj=backgroundAction.BackGroundAction(tps);
                  result.setResult(obj);
            }catch (Exception ex)
            {
                if(errorAction!=null)
                {
                    result.setCode(errorAction.ErrorCode(ex,tps));
                }
                else
                    result.setCode(HttpResult.FAIL);
            }
            return result;
        }
        throw new RuntimeException("空后台任务");

    }
}